package com.itheima;

import org.testng.annotations.Test;

import java.security.Key;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ProjectName: shixun DaErXia
 * @Titile: StreamDemo
 * @Author: 雨
 * @Description: TODO
 */
public class StreamDemo {
    public static void main(String[] args) {
        List<String>names=new ArrayList<>();
        names.add("张三1");
        names.add("张三2");
        names.add("张三");
        names.add("张三4");
        names.add("张三5");
        names.add("王五6");
        names.add("张三7");
        names.add("张三8");
        names.add("赵六");
        names.add("王五2");
        names.add("李四");
        List<String> list1=new ArrayList<>();

        names.forEach(System.out::println);
        List<String> list2=new ArrayList<>();
        list2.forEach(System.out::println);
        System.out.println("---------------");


        //1.获取集合的流对象
        //2.筛选
        names.stream().filter(s -> s.startsWith("张"))
                .filter(s->s.length()==3)
                .forEach(System.out::println);

    }
    @Test
    public void test1(){
        List<Integer> list=new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);
        //最后调佣用手机方法
/**
        List<Integer> collect = list.stream().
        filter(integer -> integer % 2 == 0).
        collect(Collectors.toList());
 **/
        List<Integer> list1 = list.stream().
                filter(integer -> integer % 2 == 0)
                .toList();
        System.out.println(list1);
    }

    public void test2(){
        List<String>list=new ArrayList<>();
        list.add("zhangsn,23");
        list.add("lisi,24");
        list.add("wangwu,26");
        //过滤年龄大于等于24
        Map<Object,Object>map=list.stream().filter(s->{
                //将s切分开
                String[] split = s.split(",");
                //拿到年龄
                int age = Integer.parseInt(split[1]);
                return age >= 24;
        }).collect(Collectors.toMap(s-> {
            String[] split=s.split("x");
            return split[0];
        },s->{
            String[] split=s.split("x");
            return split[0];
        }));
        map.forEach((k,v)->{
                System.out.println(k+"--"+v);
        });

    }
    public void test3(){
        List<String>list1=new ArrayList<>();
        List<String>list2=new ArrayList<>();
        list1.add("");
        list1.add("");
        list1.add("");
        list1.add("");
        list1.add("");
        list1.add("");
        Stream<String>stream1=list1.stream().filter(new Predicate<String>(){
            @Override
            public boolean test(String s){
                return s.length()==3;
            }
        }).limit(2);
        Stream<String>stream2=list2.stream().filter(new Predicate<String>(){
            @Override
            public boolean test(String s) {
                return s.startsWith("杨");
            }
        }).skip(1);
        Stream.concat(stream1,stream2).forEach(System.out::println);
    }
}
